﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZJFramework10.Extension;
using DevExpress.Web.ASPxEditors;
using TrainingsApp.Model;

namespace TrainingsApp.IPADApp.Master {
    public partial class TeacherEdit : PageBase {


        public int UserId {
            get {

                return ZJFramework10.Utility.QueryStringManager.GetValue<int>("uid");
            }
        }

        private TS_User_Teacher_Entity EditEntity;

        private List<TS_User_Teacher_Entity> EditList;

        protected void Page_Load(object sender, EventArgs e) {

            this.cbxCourse.DataSource = TrainingsApp.Logic.TS_CourseInfo_Logic.TS_CourseInfo_ListByPaging(100, 1).TS_CourseInfo_List;
            this.cbxCourse.ValueField = "Idx";
            cbxCourse.ValueType = typeof(int);
            this.cbxCourse.TextField = "CourseName";
            this.cbxCourse.DataBind();

            this.cbxPosition.DataSource = TrainingsApp.Logic.TS_School_Position_Logic.TS_School_Position_ListByPaging(100, 1).TS_School_Position_List;
            this.cbxPosition.ValueField = "Idx";
            cbxPosition.ValueType = typeof(int);
            this.cbxPosition.TextField = "PositionName";
            this.cbxPosition.DataBind();

            this.cbxTitle.DataSource = TrainingsApp.Logic.TS_School_Title_Logic.TS_School_Title_ListByPaging(100, 1).TS_School_Title_List;
            this.cbxTitle.ValueField = "Idx";
            cbxTitle.ValueType = typeof(int);
            this.cbxTitle.TextField = "TitleName";
            this.cbxTitle.DataBind();

            if (UserId > 0) {
                EditList = TrainingsApp.Logic.TS_User_Teacher_Logic.TS_User_Teacher_View(UserId);
                EditEntity = EditList.FirstOrDefault();
                if (!IsPostBack) {
                    if (EditEntity != null) {
                        this.btnResetPwd.Visible = true;
                        this.txtPassword.ClientVisible = false;
                        this.btnResetPwd.Visible = true;
                        this.txtUserName.Text = EditEntity.UserName;
                        this.txtUserName.ReadOnly = true;
                        if (EditEntity.BirthDay.HasValue)
                            this.txtBirthDay.Date = EditEntity.BirthDay.Value;
                        this.txtIdCard.Text = EditEntity.IdentityCard;
                        this.txtMail.Text = EditEntity.Email;
                        this.txtMobile.Text = EditEntity.Mobile;
                        this.txtName.Text = EditEntity.Name;
                        this.txtPassword.Text = EditEntity.Password;
                        this.rbtnMale.Checked = EditEntity.Gender.HasValue && EditEntity.Gender.Value;
                        this.rbtnFmale.Checked = !this.rbtnMale.Checked;
                        this.cbxCourse.Value = EditEntity.CourseId.Value;
                        this.cbxPosition.Value = EditEntity.PositionId;
                        this.hidSchoolIdx.Value = EditEntity.SchoolId.Value.ToString();
                        this.cbxTitle.Value = EditEntity.TitleId;
                        var schoolEntity = TrainingsApp.Logic.TS_School_Logic.TS_School_View(EditEntity.SchoolId.Value);
                        if (schoolEntity != null)
                            this.cbxSchool.Text = schoolEntity.SchoolName;
                        if (EditEntity.ClassesId.HasValue) {
                            var classDataSource = TrainingsApp.Logic.TS_Class_Logic.TS_Class_ListBySchoolId((int)EditEntity.SchoolId.Value);
                            this.cbxClass.DataSource = classDataSource;
                            this.cbxClass.ValueField = "Idx";
                            this.cbxClass.ValueType = typeof(Int64);
                            this.cbxClass.TextField = "DropdownName";
                            this.cbxClass.DataBind();

                            foreach (ListEditItem listItem in this.cbxClass.Items) {
                                if (EditList.Count(u => u.ClassesId == GetComboValue(listItem.Value)) > 0)
                                    listItem.Selected = true;
                            }
                        }

                        if (EditEntity.MaterialsId.HasValue) {
                            var materialsDataSource = TrainingsApp.Logic.TS_TeachMaterials_Logic.TS_TeachMaterials_ListByCourse((int)EditEntity.CourseId.Value);
                            this.cbxMaterials.DataSource = materialsDataSource;
                            this.cbxMaterials.ValueField = "Idx";
                            this.cbxMaterials.ValueType = typeof(Int32);
                            this.cbxMaterials.TextField = "Name";
                            this.cbxMaterials.DataBind();

                            this.cbxMaterials.Value = EditEntity.MaterialsId.Value;
                        }
                    }
                }
            }

        }

        protected void cbxMaterials_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e) {
            int courseId = e.Parameter.AsInt();
            this.cbxMaterials.DataSource = TrainingsApp.Logic.TS_TeachMaterials_Logic.TS_TeachMaterials_ListByCourse(courseId);
            this.cbxMaterials.ValueField = "Idx";
            this.cbxMaterials.ValueType = typeof(Int64);
            this.cbxMaterials.TextField = "Name";
            this.cbxMaterials.DataBind();
        }

        protected void cbxClass_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e) {
            int schoolId = e.Parameter.AsInt();
            this.cbxClass.DataSource = TrainingsApp.Logic.TS_Class_Logic.TS_Class_ListBySchoolId(schoolId);
            this.cbxClass.ValueField = "Idx";
            this.cbxClass.ValueType = typeof(Int64);
            this.cbxClass.TextField = "DropdownName";
            this.cbxClass.DataBind();
        }


        protected void cbxSchoolList_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e) {
            string temp = e.Parameter;
            if (!string.IsNullOrEmpty(temp)) {
                var cbxSchoolList = (cbxSchool.FindControl("cbxSchoolList") as ASPxListBox);
                cbxSchoolList.DataSource = TrainingsApp.Logic.TS_School_Logic.TS_School_ListToSearch(temp);
                cbxSchoolList.ValueType = typeof(Int64);
                cbxSchoolList.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            List<TS_User_Teacher_Entity> saveList = new List<TS_User_Teacher_Entity>();
            foreach (var item in this.cbxClass.SelectedValues) {
                if (UserId > 0) {
                    long userId = EditEntity.UserId.Value;
                    string userName = EditEntity.UserName;
                    string userpwd = EditEntity.Password;
                    EditEntity = new TS_User_Teacher_Entity();
                    EditEntity.UserId = userId;
                    EditEntity.UserName = userName;
                    EditEntity.Password = userpwd;
                }
                else {
                    EditEntity = new TS_User_Teacher_Entity();
                    EditEntity.UserName = this.txtUserName.Text;
                    EditEntity.Password = this.txtPassword.Text;
                }
                EditEntity.BirthDay = this.txtBirthDay.Date;

                EditEntity.ClassesId = GetComboValue(item);

                EditEntity.CourseId = GetComboValueToInt(this.cbxCourse.Value);
                EditEntity.Email = this.txtMail.Text;
                EditEntity.Gender = this.rbtnMale.Checked ? true : false;
                EditEntity.IdentityCard = this.txtIdCard.Text;
                EditEntity.ManagerStatus = this.chkIsLeader.Checked ? (byte)1 : (byte)0;
                EditEntity.MaterialsId = GetComboValueToInt(this.cbxMaterials.Value);
                EditEntity.Mobile = this.txtMobile.Text;
                EditEntity.Name = this.txtName.Text;
                EditEntity.UserType = UserSources.Valid;
                EditEntity.PhotoImage = string.Empty;
                EditEntity.PositionId = GetComboValueToInt(this.cbxPosition.Value);
                EditEntity.SchoolId = this.hidSchoolIdx.Value.AsInt64();
                EditEntity.TitleId = GetComboValueToInt(this.cbxTitle.Value);
                EditEntity.UserType = (byte)UserType.Teacher;
                saveList.Add(EditEntity);
            }



            if (UserId > 0) {
                TrainingsApp.Logic.TS_User_Teacher_Logic.TS_User_Teacher_Update(saveList);
            }
            else {
                var entity = TrainingsApp.Logic.TS_User_Teacher_Logic.TS_User_Teacher_Add(saveList);

                //TrainingsApp.Logic.Sys_Permission_Logic.T_USER_Role_Update(entity.UserId.Value, new List<TS_USER_ROLE_Entity> { 
                // new TS_USER_ROLE_Entity{
                //  ROLEID =(int) UserType.Teacher,
                //   USERID = (int)entity.UserId.Value
                // }
                //});
            }

            RedirectTo("TeacherManager.aspx", "保存成功");
        }

        private long? GetComboValue(object val) {
            long temp = val.AsInt64();
            if (temp > 0) return temp;
            else return null;
        }

        private int? GetComboValueToInt(object val) {
            int temp = val.AsInt();
            if (temp > 0) return temp;
            else return null;
        }

        protected void btnBack_Click(object sender, EventArgs e) {

            RedirectTo("TeacherManager.aspx");
        }


    }
}